package com.jdjw.modules.busgeneralapply.controller;


import com.google.common.collect.Lists;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApply;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApplyCommit;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApplySave;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApplyVo;
import com.jdjw.modules.busgeneralapply.service.BusGeneralApplyService;
import com.jdjw.modules.wfactivity.entity.WfActivity;
import com.jdjw.modules.wfactivity.service.WfActivityService;
import com.jdjw.modules.wftask.entity.WfTask;
import com.jdjw.modules.wftask.service.WfTaskService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 一般性补助救助 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-07-21
 */
@RestController
@RequestMapping("/busgeneralapply")
public class BusGeneralApplyController {

    @Autowired
    private BusGeneralApplyService  busGeneralApplyService;

    @Autowired
    private WfActivityService wfActivityService;

    @Autowired
    private WfTaskService wfTaskService;

    @ApiOperation("一般性补助救助-查询")
    @PostMapping("queryAll")
    public ResponseResult queryAll(BusGeneralApplyVo busGeneralApplyVo){
        return busGeneralApplyService.queryAll(busGeneralApplyVo);
    }


    @ApiOperation("一般性补助救助-添加")
    @PostMapping("save")
    public ResponseResult save(BusGeneralApplySave busGeneralApplySave){
        ResponseResult rrt=new ResponseResult();
        try {
            BusGeneralApply busGeneralApply=new BusGeneralApply();
            BeanUtils.copyProperties(busGeneralApplySave,busGeneralApply);
            busGeneralApply.setCreateDate(new Date());
            busGeneralApplyService.save(busGeneralApply);
            wfActivityService.createWorkFlow("YBXBZJZ",busGeneralApplySave.getDeptId(),busGeneralApply.getId(),busGeneralApplySave.getCreateBy(),busGeneralApplySave.getUserName());
            rrt.setCode(200);
            rrt.setMsg("保存成功");
        }catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("保存失败");
        }
        return rrt;
    }

    @ApiOperation("一般性补助救助-编辑")
    @PostMapping("edit")
    public ResponseResult edit(@RequestBody String sqlEdit) throws JSONException {
        JSONObject json=new JSONObject(sqlEdit);
        return busGeneralApplyService.sqlEdit(json.getString("sqlEdit"));
    }

    @ApiOperation("一般性补助救助-删除")
    @PostMapping("delete")
    public ResponseResult delete(String id){
        ResponseResult rrt=new ResponseResult();
        boolean b = busGeneralApplyService.removeById(id);
        if(b){
            rrt.setMsg("删除成功");
            rrt.setCode(200);
        }else{
            rrt.setMsg("删除失败");
            rrt.setCode(500);
        }
        return rrt;
    }

    @ApiOperation("一般性补助救助-查看")
    @PostMapping("{id}/preview")
    public ResponseResult preview(@PathVariable("id") String id){
        ResponseResult rrt=new ResponseResult();
        try {
            BusGeneralApply apply = busGeneralApplyService.getById(id);
            System.out.println(apply);
            Map<String,Object> map=new HashMap<>();
            map.put("apply_id", apply.getId());
            List<WfActivity> wfActivities = (List<WfActivity>) wfActivityService.listByMap(map);
            List<WfTask> taskList = Lists.newArrayList();
            if (wfActivities != null && wfActivities.size() > 0) {
                taskList = wfTaskService.queryByactivityId(wfActivities.get(0).getId());
                apply.setTaskList(taskList);
            }
            String nodeCode = wfActivityService.findNodeCode(apply.getId());
            apply.setNodeCode(nodeCode);
            rrt.setCode(200);
            rrt.setMsg("查看成功");
            rrt.setData(apply);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("查看失败");
            rrt.setData(null);
        }
        return rrt;
    }

    @ApiOperation("一般性补助救助-提交")
    @PostMapping("commit")
    public ResponseResult commit(@RequestBody BusGeneralApplyCommit busGeneralApplyCommit){
        ResponseResult rrt=new ResponseResult();

        String deptId2 = busGeneralApplyCommit.getDeptId();
        BusGeneralApply byId = busGeneralApplyService.getById(busGeneralApplyCommit.getId());
        String nodeCode = wfActivityService.findNodeCode(byId.getId());

        // 救助款发放数据保存
        if ("JZKFF".equals(nodeCode)) {
            busGeneralApplyService.updateById(byId);
        }

        rrt.setCode(200);
        rrt.setMsg("提交成功");
        try {
            WfTask wfTask = busGeneralApplyCommit.getWfTask();
            if (wfTask == null) {
                wfTask = new WfTask();
            }
            wfTask.setAttIds(busGeneralApplyCommit.getWfTask().getAttIds());
            String dualOpinion = StringUtils.isBlank(wfTask.getDualOpinion()) ? "01" : wfTask.getDualOpinion();
            BusGeneralApply busApply1 = busGeneralApplyService.getById(byId.getId());
            if ("01".equals(dualOpinion)) {  // 同意
                String[] userIdS = new String[]{busApply1.getCreateBy()};
                if ("SZZXQR".equals(nodeCode)) { // 三资确认发放到经办人
                    wfActivityService.sendNextNoteDesingnatedPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), userIdS, wfTask);
                }else if ("JZKFF".equals(nodeCode)) { // 救助款发放最后归档
                    busApply1.setPassFlag("2");
                    busGeneralApplyService.updateById(busApply1);
                    wfActivityService.sendNextNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
                }else{
                    wfActivityService.sendNextNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
                }
            } else if ("02".equals(dualOpinion)) { // 不同意
                busApply1.setPassFlag("1");
                busGeneralApplyService.updateById(busApply1);
                wfActivityService.sendAppointNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, "GD", wfTask);
            } else if ("04".equals(dualOpinion)) { // 退回
                wfActivityService.sendBack(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
            }
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(200);
            rrt.setMsg("提交失败");
        }


        return rrt;
    }

    @ApiOperation("一般性补助救助-是否提交")
    @PostMapping("submitted")
    public ResponseResult submitted(String id){
        ResponseResult rrt=new ResponseResult();
            int checkFlag = wfActivityService.findActivitySubmit(id);
            rrt.setCode(200);
            rrt.setMsg("返回结果");
            rrt.setData(checkFlag);//0:未提交，1：已提交
        return rrt;
    }


}

